package exammanager;

import gui.CustomJButton;
import gui.GUIMain;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import questionmanager.GUIQuestionDisplay;
import rmi.Question;

public class GUIExamManagerRight extends JPanel{

	private static final long serialVersionUID = 1L;
	
	private GUIExamManager parent;
	private GUIQuestionDisplay display;
	private CustomJButton addButton;
	private CustomJButton delButton;
	private JPopupMenu menu;
	private int currentClickedRow;
	private int examID;
	
	public GUIExamManagerRight(GUIExamManager parent) {
		this.parent = parent;
		setOpaque(false);
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		display = new GUIQuestionDisplay(GUIQuestionDisplay.FROM_EXAM,false,false);
		createPopupMenu();
		
		JPanel bottomPanel;
		bottomPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		bottomPanel.setOpaque(false);
		addButton = new CustomJButton("Thêm", 100, 30, Color.WHITE, CustomJButton.DARKER,
				Color.darkGray, 2, false, true);
		addButton.addActionListener(new AddQuestionAction());
		addButton.setEnabled(false);
		delButton = new CustomJButton("Xoá", 100, 30, Color.WHITE, CustomJButton.DARKER,
				Color.darkGray, 2, false, true);
		delButton.addActionListener(new DeleteQuestionAction());
		delButton.setEnabled(false);
		
		bottomPanel.add(addButton);
		bottomPanel.add(delButton);
		add(bottomPanel, BorderLayout.SOUTH);
		
		display.getJTable().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		display.getJTable().addMouseListener(new QuestionMouseListener());
		display.getJTable().getSelectionModel().addListSelectionListener(new QuestionSelectionListener());
		add(display, BorderLayout.CENTER);
	}
	
	public void setAddButtonEnable(boolean isEnable){
		addButton.setEnabled(isEnable);
	}
	
	public void refresh(int exam){
		this.examID = exam;
		if(exam == -1){
			display.getQuestionFromDB(null);
			return;
		}
		display.getQuestionFromDB(new int[]{examID});
	}
	public GUIExamManager getParentEx(){
		return this.parent;
	}
	
	private void createPopupMenu(){
		menu  = new JPopupMenu();
		JMenuItem menuItem;
		menuItem = new JMenuItem("Xoá");
		menuItem.addActionListener(new QuestionDelete());
		menu.add(menuItem);
	}
	
	class AddQuestionAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				GUIQuestionAdd addqt = new GUIQuestionAdd(parent.getFrame(),examID);
				addqt.addWindowListener(new WindowAdapter() {
					@Override
					public void windowClosing(WindowEvent e) {
						super.windowClosing(e);
						parent.getLeftPanel().refresh();
						refresh(examID);
					}
				});
				addqt.setSize(700, 500);
				addqt.setLocationRelativeTo(parent);
				addqt.setVisible(true);
			} catch (Exception e) {
				e.printStackTrace();
				return;
			}
		}
	}
	
	class DeleteQuestionAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			int result = JOptionPane.showConfirmDialog(parent, "Bạn có chắc chắn muốn xoá các câu hỏi được chọn?", "Xoá", JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE);
			if (result == JOptionPane.YES_OPTION) {
				boolean doneAll = true;
				ArrayList<Question> qIDList = new ArrayList<>();
				int[] selectedRow =  display.getJTable().getSelectedRows();
				for(int i =0;i<selectedRow.length; i++){
					Question temp = (Question) display.getModel().getValueAt(selectedRow[i], 0);
					qIDList.add(temp);
				}
				for(Question count : qIDList){
					if(GUIMain.getDb().DeleteExamDetailByExamIDandQID(examID, count.getQuestionID())){
					}else{
						doneAll = false;
						JOptionPane.showMessageDialog(parent, "Xoá câu hỏi" +count.getQuestion() + " không thành công", "Lỗi", JOptionPane.ERROR_MESSAGE);
					}
				}
				refresh(examID);
				parent.getLeftPanel().refresh();
				if(doneAll){
					JOptionPane.showMessageDialog(parent, "Xoá danh sách câu hỏi thành công", "Thành công", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}
	
	class QuestionDelete extends AbstractAction{

		private static final long serialVersionUID = 1L;
		
		@Override
		public void actionPerformed(ActionEvent e) {
			Question ex = (Question) display.getModel().getValueAt(currentClickedRow, 0);
			Object[] options = {"Có",
                    "Không"};
			String content = "Bạn thực sự muốn xoá câu hỏi này trong đề thi?";
			int n = JOptionPane.showOptionDialog(getParent(),
				    content,
				    "Chú ý",
				    JOptionPane.YES_NO_OPTION,
				    JOptionPane.QUESTION_MESSAGE,
				    null,
				    options,
				    options);
			if(n == JOptionPane.YES_OPTION){
				if(GUIMain.getDb().DeleteExamDetailByExamIDandQID(examID, ex.getQuestionID())){
					JOptionPane.showMessageDialog(parent, "Xoá câu hỏi thành công", "Thành công", JOptionPane.INFORMATION_MESSAGE);
				}else{
					JOptionPane.showMessageDialog(parent, "Xoá câu hỏi không thành công", "Lỗi", JOptionPane.ERROR_MESSAGE);
				}
				refresh(examID);
				parent.getLeftPanel().refresh();
			}
		}
	}
	class QuestionMouseListener extends MouseAdapter{
		@Override
		public void mouseClicked(MouseEvent e) {
			switch(e.getModifiers()) {
				case InputEvent.BUTTON1_MASK: {  
					super.mouseClicked(e);	
					break;
		        }
				case InputEvent.BUTTON2_MASK: {    
					break;
		        }
				case InputEvent.BUTTON3_MASK: {
					int temp = display.getJTable().rowAtPoint(e.getPoint());
					if(temp == -1) return;
			        currentClickedRow = temp;
			        menu.show(e.getComponent(), e.getX(), e.getY());
			        break;
		        }
			}
		}
	}
	
	class QuestionSelectionListener implements ListSelectionListener{

		@Override
		public void valueChanged(ListSelectionEvent e) {
			if(!e.getValueIsAdjusting()){
				int[] temp = display.getJTable().getSelectedRows();
				if(temp.length == 0){
					delButton.setEnabled(false);
					display.getCellEditor().stopCellEditing();
				}else{
					delButton.setEnabled(true);
				}
			}
		}
	}
}
